//
//  BXGCommentCell.m
//  CommunityPrj
//
//  Created by apple on 2017/9/1.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "BXGCommentCell.h"
#import "BXGCommentDataModel.h"
#import "BXGCommentReplyCell.h"
#import "BXGCommentViewModel.h"
#import "BXGCommentReplyViewModel.h"
#import "BXGNineGridLayoutHelper.h"

@interface BXGCommentCell()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, weak) BXGCommentViewModel *commentViewModel;

@property(nonatomic, strong) NSMutableDictionary<NSString*/*commentReplyId*/, BXGCommentReplyViewModel*> *dictCommentReplyViewModel;

@end

@implementation BXGCommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.replyTableView.dataSource = self;
    self.replyTableView.delegate =self;
    self.replyTableView.scrollEnabled = NO;
    
//    [self.replyTableView registerNib:[UINib nibWithNibName:@"BXGCommentReplyCell" bundle:nil] forCellReuseIdentifier:@"BXGCommentReplyCell"];
    [self.replyTableView registerClass:[BXGCommentReplyCell class] forCellReuseIdentifier:@"BXGCommentReplyCell"];
//    self.replyTableView.layer.borderColor = [UIColor blueColor].CGColor;
//    self.replyTableView.layer.borderWidth = 1;
    
    _dictCommentReplyViewModel = [NSMutableDictionary new];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)updateConstraints
{
    [super updateConstraints];
}

-(void)setupCell:(BXGCommentViewModel*)commentVM
{
    _commentViewModel = commentVM;
    
    _txtLabel.text = commentVM.dataModel.text;
    if(commentVM.dataModel.arrMediaImage && commentVM.nineGridLayout && commentVM.nineGridLayout.arrGridFrame)
    {
        [_nineGridView setImages:commentVM.dataModel.arrMediaImage andViews:commentVM.nineGridLayout.arrGridFrame];
    }
    
    BOOL bNeedUpdate = NO;
    if(_replyTableView.frame.size.height != commentVM.replyTableViewFrame.size.height)
    {
        bNeedUpdate = YES;
    }
    
    _txtLabel.frame = commentVM.txtFrame;
    //_txtLabel.backgroundColor = [UIColor redColor];
    _nineGridView.frame = commentVM.contentMediaFrame;
    //_nineGridView.backgroundColor = [UIColor greenColor];
    _replyTableView.frame = commentVM.replyTableViewFrame;
    //_replyTableView.backgroundColor = [UIColor blueColor];
    
    if(bNeedUpdate)
    {
        [self.replyTableView reloadData];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _commentViewModel.dataModel.arrReplyContent.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BXGCommentReplyViewModel *replyViewModel = [self curRenderCellViewModelAtIndex:indexPath.row];
    if(!replyViewModel)
    {
        return nil;
    }
    BXGCommentReplyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BXGCommentReplyCell" forIndexPath:indexPath];
    [cell setupCell:replyViewModel];
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BXGCommentReplyViewModel *replyViewModel = [self curRenderCellViewModelAtIndex:indexPath.row];
    if(!replyViewModel)
    {
        return 0;
    }
    return replyViewModel.cellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.sendBlock)
    {
        self.sendBlock(self.index);
    }
    BXGCommentDataModel *commentReplyDataModel = _commentViewModel.dataModel.arrReplyContent[indexPath.row];
    BXGCommentReplyViewModel *commentReplyVM = [_dictCommentReplyViewModel objectForKey:commentReplyDataModel.commentId];
    if(commentReplyVM)
    {
        [_dictCommentReplyViewModel removeObjectForKey:commentReplyDataModel.commentId];
    }
}

-(BXGCommentReplyViewModel*)curRenderCellViewModelAtIndex:(NSInteger)index
{
    if(!_commentViewModel || !_commentViewModel.dataModel.arrReplyContent || index>=_commentViewModel.dataModel.arrReplyContent.count)
        return nil;
    
    BXGCommentDataModel *commentReplyDataModel = _commentViewModel.dataModel.arrReplyContent[index];
    BXGCommentReplyViewModel *commentReplyVM = [_dictCommentReplyViewModel objectForKey:commentReplyDataModel.commentId];
    if(!commentReplyVM)
    {
        commentReplyVM = [[BXGCommentReplyViewModel alloc] init];
        commentReplyVM.dataModel = commentReplyDataModel;
        [_dictCommentReplyViewModel setObject:commentReplyVM forKey:commentReplyDataModel.commentId];
    }
    return commentReplyVM;
}

@end
